package com.lloydfinch.mvvm;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.lloydfinch.bblayer.UserBean;
import com.lloydfinch.repolayer.UserRepository;

import java.util.List;

public class MainViewModel extends ViewModel {
    private final UserRepository userRepository = new UserRepository();

    private MutableLiveData<List<UserBean>> mUserBeans = new MutableLiveData<>();

    public MainViewModel() {
        mUserBeans.setValue(userRepository.getVipUsers());
    }

    public MutableLiveData<List<UserBean>> getUserBeans() {
        return mUserBeans;
    }

    public void addUser(UserBean userBean) {
        // 先更新
        userRepository.addUser(userBean);
        // 再通知UI
//        mUserBeans.setValue(userRepository.getVipUsers()); 应该这么写的 这里没有实现数据库，直接下面这么写
        mUserBeans.getValue().add(userBean);
        mUserBeans.setValue(mUserBeans.getValue());
    }
}
